import { CustomTableInterface } from '@/components/ProTable/interface';
import { PublicInterface } from '@/interface/public';
import { useAddForm } from './use-addForm';
export function useDataGrid(proTableBox: PublicInterface.ProTableValue) {
  const refreshData = () => {
    proTableBox.value.getTableList();
    treeMapIds.forEach((value, key) => {
      console.log(key, 'key', value);

      const params = {
        ...dataGrid.initParam,
        pid: key
      };
      taskFindList(params).then(data => {
        setChildrenFlag(data);
        proTableBox.value.element.updateKeyChildren(key, data);
      });
    });
  };
  const { addForm } = useAddForm(refreshData);
  let dataGrid = reactive<CustomTableInterface>({
    initParam: {
      pid: 0
    },

    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'right', width: 260 },
      {
        prop: 'taskName',
        label: '账号',
        width: 160,
        align: 'left',
        search: {
          el: 'input',
          key: 'key',
          label: 'keyValue',
          props: {
            placeholder: '关键字:任务名称,备注'
          }
        }
      },
      {
        isShow: false,
        prop: 'isPenetrate',
        label: '查询子任务',
        enum: statusYesOrNo,
        search: {
          defaultValue: 0,
          el: 'select',
          props: {
            placeholder: '是否查询子任务'
          }
        }
      },
      {
        prop: 'status',
        label: '状态',
        enum: statusTask,
        tag: true,
        search: {
          el: 'select',
          defaultValue: 1
        }
      },

      {
        prop: 'planStart',
        label: '计划开始日期',
        width: 160
      },
      {
        prop: 'planEnd',
        label: '计划结束日期',
        width: 160
      },
      {
        prop: 'startTime',
        label: '开始时间',
        width: 160
      },

      {
        prop: 'endTime',
        label: '结束时间',
        width: 160
      },
      {
        prop: 'principalId',
        label: '负责人'
      },
      {
        prop: 'executorId',
        label: '执行人'
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: '新增任务',
        type: 'primary',
        icon: 'el-Plus',
        // permissionCode: 'p:auth:user:save',
        methods: async () => {
          formOpen(addForm, '新增任务');
          formChangeItemStatus(addForm, ['pName'], true, 'hide');
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: row => {
          formOpen(addForm, '编辑任务', row);
          formChangeItemStatus(addForm, ['pName'], true, 'hide');
        },
        attrs: {
          link: true
        }
      },
      {
        label: '新增子任务',
        type: 'primary',
        display: row => row.status < 8,
        // permissionCode: 'p:auth:user:save',
        methods: row => {
          const params = {
            pid: row.id,
            pName: row.taskName,
            principalId: row.principalId
          };
          formOpen(addForm, '新增子任务', params);
          formChangeItemStatus(addForm, ['pName'], false, 'hide');

          console.log(1);
        },
        attrs: {
          link: true
        }
      },
      {
        label: '删除',
        type: 'danger',
        display: row => row.status === 0,
        methods: row => {
          const params = { id: row.id };
          useHandleData(taskDelete, params, `${transitionLanguages('table', '删除')}【${row.taskName}】`, refreshData);
        },
        attrs: {
          link: true
        }
      },
      {
        label: '更多',
        type: 'primary',
        icon: 'ArrowDown',
        attrs: {
          link: true
        },
        children: [
          {
            label: '暂停任务',
            display: row => row.status < 5,
            methods: row => {
              const params = { id: row.id, status: 5 };
              useHandleData(taskUpdateStatus, params, `暂停任务${row.taskName}】`, refreshData);
            },
            attrs: {
              link: true
            }
          },
          {
            label: '开始任务',
            display: row => row.status === 0 || row.status === 5,
            methods: row => {
              const params = { id: row.id, status: 1 };
              useHandleData(taskUpdateStatus, params, `开始${row.taskName}】`, refreshData);
            },
            attrs: {
              link: true
            }
          },
          {
            label: '取消任务',
            display: row => row.status === 5 || row.status === 1,
            methods: row => {
              const params = { id: row.id, status: 9 };
              useHandleData(taskUpdateStatus, params, `取消${row.taskName}】`, refreshData);
            },
            attrs: {
              link: true
            }
          },
          {
            label: '结束任务',
            display: row => row.status === 5 || row.status === 1,
            methods: row => {
              const params = { id: row.id, status: 8 };
              useHandleData(taskUpdateStatus, params, `结束${row.taskName}】`, refreshData);
            },
            attrs: {
              link: true
            }
          },
          {
            label: '作废任务',
            display: row => row.status === 5 || row.status === 1,
            methods: row => {
              const params = { id: row.id };
              useHandleData(taskCancellation, params, `作废【${row.taskName}】`, refreshData);
            },
            attrs: {
              link: true
            }
          }
        ]
      }
    ]
  });

  /**
   * 当前已展开的树结构
   */
  let treeMapIds = new Map();

  const loadDataGrid = (row, treeNode, resolve) => {
    treeMapIds.set(row.id, 1);
    const params = {
      ...dataGrid.initParam,
      pid: row.id
    };
    taskFindList(params).then(data => {
      setChildrenFlag(data);
      resolve(data);
    });
  };

  const expandChange = (row: any, expandedRows: any[]) => {
    // 关闭后不再记录对应树结构
    if (!expandedRows) {
      treeMapIds.delete(row.id);
    }
  };

  /**
   *
   * @param data 数据返回
   */
  const dataCallBack = data => {
    setChildrenFlag(data.records);
    return data;
  };

  /**
   * 设置是否有子数据标识
   */
  const setChildrenFlag = data => {
    for (let item of data) {
      item.hasChildren = item.sonCount > 0;
    }
  };

  return { dataGrid, addForm, loadDataGrid, expandChange, dataCallBack };
}
